.key WIDTH/K/N,HEIGHT/K/N,FORCE/K/S,CRUSH/K/S,FILENAME/F
.bra (
.ket )

FailAt 11                                      ; Set last=" gives RC=10

Set options ""                                 ; ImageConverter options

If NOT "(WIDTH)" EQ ""
   Set options " -w (WIDTH)"                   ; max width for scaling
EndIf

If NOT "(HEIGHT)" EQ ""
   Set options "$options -h (HEIGHT)"          ; max height for scaling
EndIf

If NOT "$options" EQ ""
   If NOT "(FORCE)" EQ ""
      Set options "$options -force"            ; ignore aspect ratio
   EndIf
EndIf

If "(FILENAME)" EQ ""
   Skip geticon                                ; open the filerequester
EndIf
 
Set file ""
Set last ""
Set >NIL: last `echo "(FILENAME)" LEN 1`       ; RC=10 RC2=120 for last="
If "$last" EQ "/"
   If EXISTS "(FILENAME)"
      CD >NIL:                                 ; get current directory
      If WARN                                  ; this CD sometimes fails
         Wait 1                                ; maybe a current dir lock
      EndIf
      Set >NIL: cdir `CD`
      CD >NIL: "(FILENAME)"
      CD >NIL:                                 ; new current directory
      If WARN                                  ; this CD sometimes fails
         Wait 1                                ; maybe drawer is locked
      EndIf
      Set >NIL: drawer `CD`                    ; remove the trailing "/"
      Set >NIL: file "${drawer}.info"          ; get a drawer icon
      CD  >NIL: "$cdir"
   EndIf
Else
   If "$last" EQ ":"
      If EXISTS "(FILENAME)"
         Set >NIL: file "(FILENAME)disk.info"  ; get a disk icon
      EndIf
   Else
      Set >NIL: last `echo "(FILENAME)" LEN 5`
      If "$last" EQ ".info"                    ; disregarding case
         Set >NIL: file "(FILENAME)"           ; icon name supplied
      Else
         Set >NIL: file "(FILENAME).info"      ; get a file icon
      EndIf
   EndIf
EndIf

If EXISTS "$file"
   Skip goticon
EndIf

LAB geticon

Set file ""
Set last ""
GetEnv >NIL: SourcePath ; location of the latest successful icon loading
If WARN
   Set from "Sys:"
Else
   Set from `GetEnv SourcePath`
EndIf
Set >NIL: file `RequestFile "$from" TITLE "Get 1 Icon:" PATTERN #?.info`
IF NOT EXISTS "$file"
   Set message "Failed: no icon" ; deficon or maybe a multi-selection
   Skip error
Else
   If NOT "$file" EQ "" 
      Set >NIL: last `echo "$file" LEN 5`
      If "$last" EQ ".info"
         Skip goticon
      EndIf
   EndIf
   Set message "Failed: no icon" ; drawer or disk, but no icon selected
   Skip error
   EndIf
EndIf

LAB goticon

Set fname `List "$file" LFORMAT "%M"`
If WARN
   Set message "Invalid filename"   ; maybe parantheses in filename ?
   Skip error
EndIf

If NOT EXISTS T:NewStyle
      MakeDir T:NewStyle
EndIf

Set >NIL: icon "T:NewStyle/${fname}.info"
Copy "$file" "$icon" CLONE ; working copy with all icon attributes

If NOT EXISTS T:OldStyle
      MakeDir T:OldStyle
EndIf

If NOT EXISTS   "T:OldStyle/${fname}.info" ; don't do it again
   Copy "$file" "T:OldStyle/${fname}.info" ; this is our backup
EndIf                                      ; ==================

If NOT EXISTS T:PngImages
      MakeDir T:PngImages ; can also be used for further image processing tools
EndIf

If NOT EXISTS T:PngIcons
      MakeDir T:PngIcons  ; just joined PNG images with the name extension ".info"
EndIf

Which >NIL: ProcessIcon   ; support for old and MWB icons is an optional feature
If NOT WARN
   Set type ""
   Set >NIL: type `ProcessIcon "$icon" SHOWTYPE`
   Wait 1
   Set >NIL: type `Echo "$type" FIRST 1 LEN 8`
   IF "$type" EQ "Standard"
      ProcessIcon "$icon" MWB2CI ; convert old or MWB icons into OS 3.5 ColorIcons
   EndIf
EndIf

ImageConverter >NIL: "$icon" -of T:PngImages/%f.png   -f PNG -img 0 $options

If WARN
   Quit
EndIf

ImageConverter >NIL: "$icon" -of T:PngImages/%f_2.png -f PNG -img 1 $options

SetEnv SourcePath `List "$file" LFORMAT "%P"`

Set image  "T:PngImages/${fname}.png"
Set image2 "T:PngImages/${fname}_2.png"

If NOT "(CRUSH)" EQ ""
   Which >NIL: PngCrush
   If NOT WARN
      Set imgtmp "T:${fname}.png"
      Copy "$image" "$imgtmp"
      PngCrush -s -brute -c 6 -l 9 -w 32 -fix -noreduce -noforce "$imgtmp" "$image"
      If EXISTS "$image2"
         Copy "$image2" "$imgtmp"
         PngCrush -s -brute -c 6 -l 9 -w 32 -fix -noreduce -noforce "$imgtmp" "$image2"
      EndIf
      Delete "$imgtmp"
   EndIf	
EndIf

If EXISTS "$image2"
     Join "$image" "$image2" AS "T:PngIcons/${fname}.info"
Else
     Copy "$image"           TO "T:PngIcons/${fname}.info"
EndIf

CopyIcon "T:PngIcons/$fname" "T:NewStyle/$fname" ; supply icon with original attributes

If WARN
   Quit
EndIf

Copy "$icon" "$file" CLONE ; replace the source icon, comment this line out to keep it

Run >CON:50/25/300/75/ConvertToPngIcon/AUTO/CLOSE Wait 0 +
                                                  Echo "*N ${fname}.info*N*N converted" +
                                                  Wait 3
Quit

LAB error

Run >CON:50/25/200/25/ConvertToPNG/AUTO/CLOSE Wait 0 +
                                              Echo "*N $message" +
                                              Wait 3

 
 
